import json
import urllib
import urllib.request
from usercenter.models import CustomerProfile

client_id = 'wxe0d95453c412f118'  # 你自己的
client_key = 'd785bt925fbc7ebed62734cfdpe5951c'  # 你自己的

redirect_uri = 'http://10.9.141.21::8000/userauth/wechat'


class OAuth_QQ():
    def __init__(self, client_id, client_key, redirect_uri):
        self.client_id = client_id
        self.client_key = client_key
        self.redirect_uri = redirect_uri

    def get_auth_url(self):
        """获取授权页面的网址"""
        params = {'client_id': self.client_id,
                  'response_type': 'code',
                  'redirect_uri': self.redirect_uri,
                  'scope': 'get_user_info',
                  'state': 1}
        url = 'https://graph.qq.com/oauth2.0/authorize?%s' % urllib.parse.urlencode(params)
        return url

    def get_access_token(self, code):
        """根据code获取access_token"""
        params = {'grant_type': 'authorization_code',
                  'client_id': self.client_id,
                  'client_secret': self.client_key,
                  'code': code,
                  'redirect_uri': self.redirect_uri}
        url = 'https://graph.qq.com/oauth2.0/token?%s' % urllib.parse.urlencode(params)

        # 访问该网址，获取access_token
        response = urllib.request.urlopen(url).read()
        str = response.decode('utf-8')
        result = urllib.parse.parse_qs(str, True)
        access_token = ''.join(result['access_token'])
        self.access_token = access_token

        openid = self.get_open_id(access_token)
        return {'access_token': access_token, 'openid': openid}

    def get_open_id(self, access_token=''):
        """获取QQ的OpenID"""
        params = {'access_token': self.access_token}
        url = 'https://graph.qq.com/oauth2.0/me?%s' % urllib.parse.urlencode(params)

        response = urllib.request.urlopen(url).read()
        v_str = response.decode()  # 去掉callback的字符
        str = v_str.replace('callback', '')
        str1 = str.replace('(', '')
        str2 = str1.replace(')', '')
        str3 = str2.replace(';', '')
        v_json = json.loads(str3)

        openid = v_json['openid']
        self.openid = openid
        return openid

    def get_qq_info(self, openid='', access_token=''):
        """获取QQ用户的资料信息"""
        params = {'access_token': access_token or self.access_token,
                  'oauth_consumer_key': self.client_id,
                  'openid': openid or self.openid}
        url = 'https://graph.qq.com/user/get_user_info?%s' % urllib.parse.urlencode(params)

        response = urllib.request.urlopen(url).read()
        return json.loads(response)


oauth_qq = OAuth_QQ(client_id, client_key, redirect_uri)


def create_user_from_qq(wx_user_info):
    '''
    创建QQ用户
    wx_user_info = {
        "openid":"OPENID",
        "nickname":"NICKNAME",
        "sex":1,
        "province":"PROVINCE",
        "city":"CITY",
        "country":"COUNTRY",
        "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
        "privilege":[
            "PRIVILEGE1",
            "PRIVILEGE2"
        ],
        "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
    }

    '''
    wx_user_info_key_map = {
        "openid": "wechat_openid",
        "nickname": "username",
    }
    user_info = {wx_user_info_key_map[key]: wx_user_info[key] for key in wx_user_info_key_map}
    user_info['wechat_user_info'] = wx_user_info
    return CustomerProfile.objects.update_or_create(defaults=user_info, wechat_openid=user_info['wechat_openid'])
